package ${scfg._web_pkg};

import ${scfg._api_pkg}.I${tcfg.tableNameFormat!}Service;
import ${scfg._dto_pkg}.${tcfg.tableNameFormat!}Dto;

import cn.wu.framework.PageUtil;
import cn.wu.framework.Response;
import cn.wu.framework.annotation.ALogOperation;
import cn.wu.framework.annotation.auth.Logical;
import cn.wu.framework.annotation.RfUser2Bean;
import cn.wu.framework.annotation.auth.RequiresPermissions;
import cn.wu.framework.util.CommonConstant;
import cn.wu.framework.util.JwtUtil;
import cn.wu.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "${tcfg.tableComment}")
@RestController
@Slf4j
public class ${tcfg.tableNameFormat!}Controller extends BaseController {
    @Autowired
    I${tcfg.tableNameFormat!}Service ${tableNameL}Service;
    static final String acPrefix = "${scfg._web_http}/";

    @[ if ("1"==scfg._page) { /]
    @RequiresPermissions("${tableNameL}:menu")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "page/{pageNum}")
    // @ApiOperation(value = "信息分页(未删除)")
    public Response page(@ModelAttribute  ${tcfg.tableNameFormat!}Dto dto, @PathVariable("pageNum") Integer pageNum) {
       log.info("page.........");
       Response result = new Response();
       try {
          dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
          dto.setPageNum(pageNum);
          @[ if (validator.notEmpty(scfg._col_del)) { /]dto.set${strUtil.toUpperCase(scfg._col_del,'_')}(0);@[ } /]
          result.data = PageUtil.copy(${tableNameL}Service.findDataIsPage(dto));
       } catch (Exception e) {
          result = Response.error(e.getMessage());
       }
       return result;
    }
    @[ } /]

    @[ if ("1"==scfg._tree) { /]
    @RequiresPermissions("${tableNameL}:menu")
    @RequestMapping(method={RequestMethod.GET,RequestMethod.POST},value=acPrefix+"tree")
    @ResponseBody
    // @ApiOperation(value = "信息树json")
    public Response jsonTree() {
       log.info("jsonTree.........");
       Response result=new Response(0,"success");
       try {
          result.data=${tableNameL}Service.findDataIsTree(new ${tcfg.tableNameFormat!}Dto());
       } catch (Exception e) {
          result=Response.error(e.getMessage());
       }
       return result;
    }
    @[ } /]


    @[ if ("1"==scfg._detail) { /]
    @RequiresPermissions("${tableNameL}:info")
    @RequestMapping(method = RequestMethod.GET, value = acPrefix + "info/@[ for(t in pks){ /]${tLP.index==1?'':'/'}{${t.columnNameFormat}}@[ } /]")
    // @ApiOperation(value = "信息详情")
    public Response info(@[ for(t in pks){ /]${tLP.index==1?'':','}@PathVariable("${t.columnNameFormat}") ${t.javaType} ${t.columnNameFormat}@[ } /]) {
       log.info("info.........");
       Response result = new Response();
       try {
          ${tcfg.tableNameFormat!}Dto dto = new ${tcfg.tableNameFormat!}Dto();
          @[ for(t in pks){ /]
             dto.set${strUtil.toUpperCaseFirstOne(t.columnNameFormat)}(${t.columnNameFormat});
          @[ } /]
          @[ if (validator.notEmpty(scfg._col_del)) { /]  dto.set${strUtil.toUpperCase(scfg._col_del,'_')}(0);@[ } /]
          result.data = ${tableNameL}Service.findDataById(dto);
       } catch (Exception e) {
          result = Response.error(e.getMessage());
       }
       return result;
    }
    @[ } /]

    @[ if ("1"==scfg._delLogic && null!=scfg._col_del) { /]
    @RequiresPermissions("${tableNameL}:del")
    @RequestMapping(method = RequestMethod.POST, value = acPrefix + "del/@[ for(t in pks){ /]${tLP.index==1?'':'/'}{${t.columnNameFormat}}@[ } /]")
    @ALogOperation(type = "删除", desc = "${tcfg.tableComment}-逻辑删除")
    // @ApiOperation(value = "逻辑删除")
    public Response del(@[ for(t in pks){ /]${tLP.index==1?'':','}@PathVariable("${t.columnNameFormat}") ${t.javaType} ${t.columnNameFormat}@[ } /]) {
       log.info("del.........");
       Response result = new Response(0,"success");
       try {
          ${tcfg.tableNameFormat!}Dto dto = new ${tcfg.tableNameFormat!}Dto();
          @[ for(t in pks){ /]
             dto.set${strUtil.toUpperCaseFirstOne(t.columnNameFormat)}(${t.columnNameFormat});
          @[ } /]
          result.message = ${tableNameL}Service.deleteDataById(dto);
       } catch (Exception e) {
          result = Response.error(e.getMessage());
       }
       return result;
    }
    @[ }else if ("1"==scfg._del) { /]
    @RequiresPermissions("${tableNameL}:phydel")
    @RequestMapping(method = RequestMethod.POST, value = acPrefix + "phydel/@[ for(t in pks){ /]${tLP.index==1?'':'/'}{${t.columnNameFormat}}@[ } /]")
    @ALogOperation(type = "删除", desc = "${tcfg.tableComment}-物理删除")
    // @ApiOperation(value = "物理删除")
    public Response phydel(@[ for(t in pks){ /]${tLP.index==1?'':','}@PathVariable("${t.columnNameFormat}") ${t.javaType} ${t.columnNameFormat}@[ } /]) {
       log.info("phydel.........");
       Response result = new Response(0,"success");
       try {
         ${tcfg.tableNameFormat!}Dto dto = new ${tcfg.tableNameFormat!}Dto();
         @[ for(t in pks){ /]
          dto.set${strUtil.toUpperCaseFirstOne(t.columnNameFormat)}(${t.columnNameFormat});
         @[ } /]
          result.message = ${tableNameL}Service.deleteData(dto);
       } catch (Exception e) {
          result = Response.error(e.getMessage());
       }
       return result;
    }
    @[ } /]

    @[ if ("1"==scfg._insert||"1"==scfg._update) { /]
    @RequiresPermissions(value = {"${tableNameL}:add", "${tableNameL}:edit"}, logical = Logical.OR)
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT}, value = acPrefix + "save")
    @RfUser2Bean
    @ALogOperation(type = "修改", desc = "${tcfg.tableComment}")
    // @ApiOperation(value = "信息保存")
    public Response save(@Validated @ModelAttribute ${tcfg.tableNameFormat!}Dto dto, BindingResult bindingResult) {
       log.info("save.........");
       Response result = new Response(0,"success");
       try {
          //if ("1".equals(request.getSession().getAttribute(acPrefix + "save." + dto.getToken()))) {
          //    throw new RuntimeException("请不要重复提交!");
          //}
          if (bindingResult.hasErrors()) {
             String errorMsg = "";
             List<ObjectError> errorList = bindingResult.getAllErrors();
             for (ObjectError error : errorList) {
                errorMsg += (error.getDefaultMessage()) + ";";
             }
             result = Response.error(errorMsg);
          } else {
             result = ${tableNameL}Service.saveOrUpdateData(dto);
             //request.getSession().setAttribute(acPrefix + "save." + dto.getToken(), "1");
          }
       } catch (Exception e) {
          result = Response.error(e.getMessage());
       }
       return result;
    }
    @[ } /]
}